/**
 * 
 */
package it.unibo.refolding.common;

import java.io.Serializable;
import java.util.Objects;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public class DoubleTriad implements Comparable<DoubleTriad>, Serializable {

	private static final long serialVersionUID = 851702665690085751L;
	
	public static final DoubleTriad[] EMPTY_ARRAY = new DoubleTriad[0];

	public DoubleTriad(double x, double y, double z) {
		this.x = x;
		this.y = y;
		this.z = z;
	}

	public static DoubleTriad of(double x, double y, double z) {
		return new DoubleTriad(x, y, z);
	}

	public int compareTo(DoubleTriad other) {
		int cmp = Double.compare(x, other.x);
		if (cmp == 0) {
			cmp = Double.compare(y, other.y);
		}
        if (cmp == 0) {
            cmp = Double.compare(z, other.z);
        }
		return cmp;
	}

	public final double x, y, z;

	@Override
	public String toString() {
		return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
	}

	@Override
	public int hashCode() {
	    return Objects.hash(x, y, z);
	}

	@Override
    public boolean equals(Object obj) {
        if (!(obj instanceof DoubleTriad)) {
            return false;
        }
        DoubleTriad other = (DoubleTriad) obj;
        return Double.doubleToLongBits(x) == Double.doubleToLongBits(other.x)
            && Double.doubleToLongBits(y) == Double.doubleToLongBits(other.y)
            && Double.doubleToLongBits(z) == Double.doubleToLongBits(other.z);
    }

	public static double sumX(DoubleTriad[] tz) {
		double sum = 0;
		for (DoubleTriad dp : tz) {
			sum += dp.x;
		}
		return sum;
	}

	public static double[] x(DoubleTriad[] tz) {
		double[] a = new double[tz.length];
		for (int i = tz.length; --i >= 0;) {
			a[i] = tz[i].x;
		}
		return a;
	}
}
